<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
</head>

<body>
<div id="header">
    <img class="logo_img" alt="" src="static/img/logo.png">
    <span class="wel_word">购物车</span>
    <%--静态包含登录成功信息--%>
    <%@include file="/pages/common/login_succeed.jsp" %>
</div>

<script type="text/javascript">
    $(function () {
        /*
        * 购物车中点击删除商品时提示用户“是否确认删除”
        * */
        $("a.deleteGoodsBtn").click(function () {
            // 获取该商品名称
            let goodsName = $(this).parent().parent().find("td:first").text();
            return confirm("您确定从购物车中删除【" + goodsName + "】吗？");
        })

        /*
        * 购物车中点击清除购物车时提示用户“是否清除购物车”
        * */
        $("#clearCartBtn").click(function () {
            return confirm("您确定清空购物车中所有商品吗？");
        })

        /*
        * 修改购物车商品数量功能
        * */
        $("input.updateCountIpt").blur(function () {
            // 获取该商品名称
            let goodsName = $(this).parent().parent().find("td:first").text();
            // 获取该商品数量
            let goodsCount = $(this).val();
            // 获取该商品的id
            let goodsId = $(this).attr("updateGoodsId");

            // 只有商品数量发生了改变，才弹出确认提示框
            if (this.defaultValue !== goodsCount) {
                // 商品数量发生改变时提醒用户是否确认修改
                if (confirm("您确定将【" + goodsName + "】数量修改为" + goodsCount + "吗？")) {
                    location.href = "cartServlet?action=updateCart&goodsId=" + goodsId + "&goodsCount=" + goodsCount;
                } else {
                    // 返回文本框的初始内容
                    $(this).val(this.defaultValue);
                }
            } else {
                return false;
            }
        })
    })
</script>

<div id="main">
    <table>
        <tr>
            <td>商品名称</td>
            <td>数量</td>
            <td>单价</td>
            <td>金额</td>
            <td>操作</td>
        </tr>

        <c:if test="${empty sessionScope.cart.cartGoods}">
            <tr>
                <td colspan="5"><a href="index.jsp">购物车竟然是空的！快去逛逛吧！</a></td>
            </tr>
        </c:if>

        <c:if test="${not empty sessionScope.cart.cartGoods}">
            <c:forEach items="${sessionScope.cart.cartGoods}" var="goods">
                <tr>
                    <td>${goods.value.name}</td>
                    <td>
                        <input class="updateCountIpt"
                               type="number" min="1"
                               value="${goods.value.count}"
                               updateGoodsId="${goods.value.id}"
                               style="width: 80px;text-align: center">
                    </td>
                    <td>${goods.value.price}</td>
                    <td>${goods.value.totalPrice}</td>
                    <td><a class="deleteGoodsBtn" href="cartServlet?action=deleteCart&deleteGoodsId=${goods.value.id}">删除</a>
                    </td>
                </tr>
            </c:forEach>
        </c:if>
    </table>
    <c:if test="${not empty sessionScope.cart.cartGoods}">
        <div class="cart_info">
            <span class="cart_span">购物车中共有<span class="b_count">${sessionScope.cart.totalCount}</span>件商品</span>
            <span class="cart_span">总金额<span class="b_price">${sessionScope.cart.totalPrice}</span>元</span>
            <span class="cart_span"><a id="clearCartBtn" href="cartServlet?action=clearCart">清空购物车</a></span>
            <span class="cart_span"><a href="orderServlet?action=createOrder">去结账</a></span>
        </div>
    </c:if>
</div>
<%--静态包含页脚信息--%>
<%@include file="/pages/common/footer.jsp" %>
</body>
</html>